package com.ccp.dev.core.util.jsonobject.support;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class BooleanSerializer implements JsonDeserializer<Boolean>, JsonSerializer<Boolean> {
    @Override
    public Boolean deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
        if (("\"1\"").equals(json.toString())) {
            return true;
        } else if (("\"0\"").equals(json.toString())) {
            return false;
        }
        return json.getAsBoolean();
    }

    @Override
    public JsonElement serialize(Boolean arg0, Type arg1, JsonSerializationContext arg2) {
        return new JsonPrimitive(arg0.toString());
    }
}
